package com.cloud.zuulapi.filter;
import com.cloud.commonapi.jsonResult.Result;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 陈楚城
 * @date 2019/10/4 12:34
 */
@Component
public class LoginFilter extends ZuulFilter {


    @Override
    public String filterType()
    {
        return "pre";
    }

    @Override
    public int filterOrder()
    {
        return 0;
    }

    @Override
    public boolean shouldFilter()
    {
        /*RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest httpServletRequest=requestContext.getRequest();
        System.out.println(httpServletRequest.getRequestURI());
        if(httpServletRequest.getRequestURI().contains("/api-a/usercontroller/userinfo/users"))
            return true;*/
        return false;
    }

    @Override
    public Object run() throws ZuulException
    {
        //书写逻辑
        RequestContext requestContext = RequestContext.getCurrentContext();
        if (requestContext.getRequest().getSession().getAttribute("loginId") != null)
        {

        }
        else
        {
            requestContext.setSendZuulResponse(false);//关闭请求，调用此方法，表示当前的请求不在往下执行
            //编写响应信息
            requestContext.setResponseStatusCode(400);//设置状态码
            requestContext.setResponseBody("未登录"); //设置响应的信息
            requestContext.getResponse().setContentType("text/html;charset=utf-8");
           //return Result.build(400,"未登录");
        }
        return null;
    }
}

